// pages/order/balance/balance.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    cartList: [],
    sumMonney: 0,//总金额
    cupNumber:0,//食品数量
    model: 0,//1是预约模式  0是到店模式
    appointTime: "",//预约的取餐时间
    note:'',//备注
    cutid:0,//优惠券相关
    cutMonney: 0,//优惠券减去金额
    cutText:"",//优惠券减金额详细
    cutid:''
  },

  onShow: function () {
    var openid = wx.getStorageSync('openId')
    this.webSocketHandleMsg(openid, this);//连接websocket
    this.getMyOrder();//获取优惠券
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var openid = wx.getStorageSync('openId')
    this.webSocketInit(openid, this);//初始化websocket
    if (options.model == 1) {//预约取餐模式
      this.setData({
        model: 1,
        appointTime: options.appointTime
      })
    }
    //动态设置当前页面的标题
    wx.setNavigationBarTitle({
      title: '订单详情'
    })
    this.setData({
      cartList: wx.getStorageSync('cartList'),
      sumMonney: wx.getStorageSync('sumMonney'),//总金额
      cupNumber: wx.getStorageSync('cupNumber'),//数量
    })
  },
  //长链接
  webSocketInit: function (openid, that) {
    wx.connectSocket({
      url: 'ws://127.0.0.1:8181?token=' + openid,
    })
  },
  //websock连接
  webSocketHandleMsg: function (openid, that) {
    // 监听 WebSocket 连接打开事件
    wx.onSocketOpen(function (res) {
      console.log('WebSocket连接已打开！')
    })
    //监听 WebSocket 错误事件
    wx.onSocketError(function (res) {
      console.log('WebSocket连接打开失败，请检查！')
    })
    //监听 WebSocket 连接关闭事件 - 心跳重连
    wx.onSocketClose(function (res) {
      //创建一个 WebSocket 连接
      wx.connectSocket({
        url: 'ws://127.0.0.1:8181?token=' + openid,
      })
      console.log('WebSocket 已关闭！')
    })
  },
  onUnload: function () {
    //关闭websocket
    wx.closeSocket()
  },
  //获取我的优惠券
  getMyOrder: function () {
    wx.showLoading();
    var that = this;
    //获取我的订单
    wx.request({
      url: app.globalData.apiHost + '/getCutList?openid=' + wx.getStorageSync('openId') + "&model=0",
      method: 'GET',
      data: {},
      header: {
        'Accept': 'application/json'
      },
      success: function (res) {
        wx.hideLoading();
        console.log(res)
        for (let i = 0; i < res.data.msg.length;i++){
          if (res.data.msg[i].status==0){
            console.log(res.data.msg[i].detail)
            if (res.data.msg[i].detail.type==1){
              if (wx.getStorageSync('sumMonney') > res.data.msg[i].detail.rule-1){
                that.setData({
                  cutText: "满" + res.data.msg[i].detail.rule + "元立减" + res.data.msg[i].detail.cut + "元",
                  cutMonney: res.data.msg[i].detail.cut,
                  cutid: res.data.msg[i].id
                })
              }
            }else{
              let list = wx.getStorageSync('cartList')
              for (let j = 0; j < list.length;j++){
                console.log(list[j].name)
                if (res.data.msg[i].detail.name == list[j].name){
                  that.setData({
                    cutText: "单品优惠立减" + res.data.msg[i].detail.cut + "元",
                    cutMonney: res.data.msg[i].detail.cut,
                    cutid: res.data.msg[i].id
                  })
                }
              }
              
              
            }
          }
        }
      }
    })
  },
  //付款
  gopay:function(){
    var that =this;
    //定时器让备注能拿得到数据
    setTimeout(function(){ 
      var nonce = Math.floor((Math.random() + Math.floor(Math.random() * 9 + 1)) * Math.pow(10, 8));//id
      nonce = nonce.toString()
      var total = that.data.sumMonney - that.data.cutMonney;//总价减去优惠券
      //支付接口
      wx.request({
        url: app.globalData.apiHost + '/wxPay?openid=' + wx.getStorageSync('openId'),
        method: 'POST',
        data:{
          "nonce_str": nonce+"a"+ total
        },
        dataType: 'json',
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res)
          if (res.data.code == 0) {
            var payModel = res.data.msg;
            //发起微信支付。调用前需在小程序微信公众平台 -功能-微信支付入口申请接入微信支付
            wx.requestPayment({
              'timeStamp': payModel.timestamp,
              'nonceStr': payModel.nonceStr,
              'package': payModel.package,
              'signType': 'MD5',
              'paySign': payModel.paySign,
              'success': function (res) {
                wx.showToast({
                  title: '支付成功',
                  icon: 'success',
                  duration: 2000
                })
                console.log("dasda", payModel.package.substr(10))
                //支付成功后 - 去下单
                that.addOrder(payModel.out_trade_no, payModel.package.substr(10));
              },
              'fail': function (res) {
  
              }
            })
          }
        },
        fail: function () {
  
        }
      })
      //这里是无论支付成功与否都去下单 - 等着删除改为失败时提示
      that.addOrder(nonce+"a"+ total,'wxTest'+nonce+"a"+ total);//去下单
    },300)

  },
  //下订单
  addOrder: function (out_trade_no,packages){
    wx.showLoading({
      title: '正在生成餐号',
    })
    var that = this;
    wx.request({
      url: app.globalData.apiHost + '/addOrder?openid=' + wx.getStorageSync('openId'), //下单
      method: 'POST',
      data: {
        out_trade_no: out_trade_no,
        cartList: wx.getStorageSync('cartList'),
        sumMonney: wx.getStorageSync('sumMonney') - that.data.cutMonney,
        cutMonney: that.data.cutMonney,
        cutText:that.data.cutText,
        cupNumber: wx.getStorageSync('cupNumber'),
        model:this.data.model,
        appointTime: this.data.appointTime,
        packages:packages,
        note:that.data.note,//备注
      },
      dataType: 'json',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res.data)
        wx.setStorageSync('orderInfo', res.data.msg);
        wx.setStorageSync('cutMoney', that.data.cutMonney);
        wx.sendSocketMessage({
          data: "newOrder"
        }) 
        // that.useCut();//使用优惠券
        wx.hideLoading();
        //跳转到订单详情页面(取餐号)
        wx.redirectTo({
          url: '../detail/detail?orderId=' + res.data.msg.orderId//附带订单号码
        })
      }
    })
  },
  //添加备注
  note:function(e){
    console.log(e.detail.value)
    this.setData({
      note: e.detail.value
    })
  },
  //使用优惠券
  useCut: function () {
    wx.showLoading();
    var that = this;
    //获取我的订单
    wx.request({
      url: app.globalData.apiHost + '/useCut?id=' + that.data.cutid,
      method: 'GET',
      data: {},
      header: {
        'Accept': 'application/json'
      },
      success: function (res) {
        wx.hideLoading();
        console.log(res)
      }
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})